import {Component, Input} from '@angular/core';
import {DocApi} from '../../entity/doc-api';
import {MockConditionRest} from '../../entity/mock-condition-rest';
import {Doc} from '../../entity/doc';

@Component({
    selector: 'app-doc-mock-condition-rest',
    templateUrl: './doc-mock-rest-condition.component.html',
    styleUrls: ['./doc-mock-rest-condition.component.css']
})
export class DocMockRestConditionComponent {
    private _api: DocApi;

    /**
     * 条件
     */
    private _condition: MockConditionRest;

    @Input()
    doc: Doc;

    conditionActiveTab = '';

    constructor() {
    }

    setTab(tab: string) {
        this.conditionActiveTab = tab;
    }

    private setActiveCondition() {
        if (this._condition.headerParams.length > 1) {
            this.conditionActiveTab = 'headers';
        }
        if (this._condition.urlParams.length > 1) {
            this.conditionActiveTab = 'params';
        }
        if (this._condition.pathParams && this._condition.pathParams.length > 1) {
            this.conditionActiveTab = 'pathParams';
        }

        if (!this.conditionActiveTab) {
            this.conditionActiveTab = 'params';
        }
    }

    get api(): DocApi {
        return this._api;
    }

    @Input()
    set api(value: DocApi) {
        this._api = value;
    }

    get condition(): MockConditionRest {
        return this._condition;
    }

    @Input()
    set condition(value: MockConditionRest) {
        if (!value) {
            return;
        }
        this._condition = value;
        this.setActiveCondition();
    }
}
